package dao.impl;

import dao.BaseDao;
import dao.IOrderDao;
import pojo.OrderDetail;
import pojo.OrderGoodsInfo;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements IOrderDao {
    /**
     * 向订单详情表中添加数据
     *
     * @param orderDetail
     * @return
     */
    @Override
    public boolean addToOrderDetail(OrderDetail orderDetail) {
        return update("insert into t_order_detail(id, uid, receipt_name, receipt_address, receipt_phone, pay_type, send_type, notes) values(?,?,?,?,?,?,?,?)",
                orderDetail.getId(), orderDetail.getUid(), orderDetail.getReceipt_name(),
                orderDetail.getReceipt_address(), orderDetail.getReceipt_phone(), orderDetail.getPay_type(),
                orderDetail.getSend_type(), orderDetail.getNotes()) == 1;
    }

    /**
     * 向订单-商品映射表中添加数据
     *
     * @param orderGoodsInfo
     * @return
     */
    @Override
    public boolean addToOrderGoodsInfo(OrderGoodsInfo orderGoodsInfo) {
        return update("insert into t_order_goods_info(oid,gid,count) values(?,?,?)",
                orderGoodsInfo.getOid(), orderGoodsInfo.getGid(), orderGoodsInfo.getCount()) == 1;
    }

    /**
     * 根据订单id从订单详情表中查询数据
     *
     * @param id
     * @return
     */
    @Override
    public OrderDetail queryOrderDetailById(String id) {
        return queryForOne(OrderDetail.class,
                "select id, uid, receipt_name, receipt_address, receipt_phone, pay_type, send_type, notes from t_order_detail where id=?",
                id);
    }

    /**
     * 根据订单id从订单-商品映射表中查询数据
     *
     * @param oid
     * @return
     */
    @Override
    public List<OrderGoodsInfo> queryOrderGoodsInfosById(String oid) {
        return queryForList(OrderGoodsInfo.class,
                "select id, oid, gid, count from t_order_goods_info where oid=?",
                oid);
    }

    /**
     * 根据用户id查询订单
     *
     * @param uid
     * @return
     */
    @Override
    public List<OrderDetail> queryOrderDetailsByUid(int uid) {
        return queryForList(OrderDetail.class,
                "select id, uid, receipt_name, receipt_address, receipt_phone, pay_type, send_type, notes,gmt_create,gmt_modified from t_order_detail where uid=? order by gmt_create desc",
                uid);
    }
}
